package com.ncp.servlet.product;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

import com.ncp.model.biz.news.NewsManager;
import com.ncp.model.biz.product.ManageBackstageProductBiz;
import com.ncp.model.entity.product.ProductAllInfoBean;

public class ManagerBasicServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		
		
		boolean b=false;
		String s[]=new String[10];
		//设置request编码，主要是为了处理普通输入框中的中文问题
		//这里对request进行封装，RequestContext提供了对request多个访问方法
		RequestContext requestContext = new ServletRequestContext(request);
		if(requestContext!=null){
		String path1 = request.getServerName();
		String path2 = request.getRealPath(path1);
		
		String url="";
		int ProduuctID=0;
		
		int n=path2.lastIndexOf("\\");
		String S_realPath=path2.substring(0,n);
		String realPath =S_realPath+"\\"; 
		//判断表单是否是Multipart类型的。这里可以直接对request进行判断，不过已经以前的用法了
		if(FileUpload.isMultipartContent(requestContext)){
		   DiskFileItemFactory factory = new DiskFileItemFactory();
		   //设置文件的缓存路径
		   factory.setRepository(new File("D:/image/"));
		   ServletFileUpload upload = new ServletFileUpload(factory);
		   //设置上传文件大小的上限，-1表示无上限 
		   upload.setSizeMax(-1);
		   List items = new ArrayList();
		   try {
		       //上传文件，并解析出所有的表单字段，包括普通字段和文件字段
			   items = upload.parseRequest(request);
		   } catch (FileUploadException e1) {
			 //  System.out.println("文件上传发生错误" + e1.getMessage());
		    }
		    //下面对每个字段进行处理，分普通字段和文件字段
		   Iterator it = items.iterator();
		   int i=-1;
		   while(it.hasNext()){
			   i++;
			   FileItem fileItem = (FileItem) it.next();
			   //如果是普通字段
			   if(fileItem.isFormField()&&i<=9){   
		    	s[i]=new String(fileItem.getString().getBytes("iso8859-1"), "UTF-8");
		    	
		    }else{
		    	
		    	
		    	String tempStr1[]=NewsManager.split(fileItem.getName(),"\\");
		    	s[i]=tempStr1[tempStr1.length-1];
		    	String tempStr2[]=NewsManager.split(s[i],".");
		    	s[i]=UUID.randomUUID().toString()+"."+tempStr2[tempStr2.length-1];
			     //保存文件，其实就是把缓存里的数据写到目标路径下
			     if(fileItem.getName()!=null && fileItem.getSize()!=0){
			    	 url="images/productimg/";
			    	 
			    		File fullFile = new File(fileItem.getName());
			     File newFile= new File(realPath+url+ s[i]);
			     
			     try {
			    	 fileItem.write(newFile);
			     } catch (Exception e) {
			    	 e.printStackTrace();
			      }
			     }else{
			    	 System.out.println("文件没有选择 或 文件内容为空");
			     }
			 }
		
		   }
		}
		   
	
		   if(s[0].length()!=0&&s[1].length()!=0&&s[2].length()!=0&&s[3].length()!=0&&s[4].length()!=0&&s[5].length()!=0&&s[6].length()!=0&&s[7].length()!=0&&s[8].length()!=0){
		
			Float RetailPrice = Float.parseFloat(s[3]);
			Float MarketPrice = Float.parseFloat(s[4]);
		    int CurrentPNumber = Integer.parseInt(s[8]);
			Float ProductDiscount= Float.parseFloat(s[6]);
			int  ProductSortID =  Integer.parseInt(s[0]);
			ProductAllInfoBean paf = new ProductAllInfoBean();
			paf.setProductSortID(ProductSortID);
			paf.setProductName(s[2]);
			String 	ProductImage="images/productimg/No.jpg";
			paf.setProductImage(ProductImage);
			paf.setRetailPrice(RetailPrice);
			paf.setMarketPrice(MarketPrice);
			paf.setProductType(s[5]);
			paf.setProductDiscount(ProductDiscount);
			paf.setEndTime(s[7]);
			paf.setCurrentPNumber(CurrentPNumber);
			paf.setProductDescription(s[1]);
			ManageBackstageProductBiz mbf = new ManageBackstageProductBiz();
			
			boolean b1=false;
			
			b1=mbf.setProductBasic(paf);
			if(b1==true){
				String pages="ManagerShowBasicServlet?type=add";
				request.setAttribute("pages",pages);
				request.getRequestDispatcher("pages/admin/product/suc.jsp").forward(request,response);
				
			}}
			else{
				String pages="请填入完整信息";
				request.setAttribute("pages",pages);
				request.getRequestDispatcher("pages/admin/product/err.jsp").forward(request,response);
			}
		}	
	}

}
